package com.nbot.server.algorithm;

import com.nbot.server.domain.AlarmpointDO;
import com.nbot.server.domain.AnalogalarmDO;
import com.nbot.server.domain.RelevancealarmDO;

/**
 * 均值滤波
 * wyzc 180606
 */

public class MovingAverageImpl {

    SetNumValue FormatResult = new SetNumValue();

    public AlarmpointDO getBoolMAvg(AlarmpointDO Ap, String tmp) {

        String[] data = {Ap.getVf(),Ap.getVs(),Ap.getVt(),tmp};

        float Sum = 0f;
        int num = 0;

        for (String tar : data){
            if(null != tar && !tar.equals("")){
                float val = Float.parseFloat(tar);
                Sum += val;
                num++;
            }
        }

        int result = Math.round(Sum/num);

        Ap.setVf(Ap.getVs());
        Ap.setVs(Ap.getVt());
        Ap.setVt(tmp);
        Ap.setTmp(String.valueOf(result));

        return Ap;
    }

    public AnalogalarmDO getAnalogMAvg(AnalogalarmDO Ag, String tmp) {

        String[] data = {Ag.getValuefirst(),Ag.getValuesecond(),Ag.getValuethird(),tmp};

        float Sum = 0f;
        int num = 0;

        for (String tar : data){
            if(null != tar && !tar.equals("")){
                float val = Float.parseFloat(tar);
                Sum += val;
                num++;
            }
        }

        float result = Sum/(float)num;

        Ag.setValuefirst(Ag.getValuesecond());
        Ag.setValuesecond(Ag.getValuethird());
        Ag.setValuethird(tmp);
        Ag.setValuefourth(String.valueOf(result));

        return Ag;
    }

    public RelevancealarmDO getRelevanceMAvg(RelevancealarmDO ra, String tmp) {

        String[] data = {ra.getVf(),ra.getVs(),ra.getVt(),tmp};

        float Sum = 0f;
        int num = 0;

        for (String tar : data){
            if(null != tar && !tar.equals("")){
                float val = Float.parseFloat(tar);
                Sum += val;
                num++;
            }
        }

        float result;

        if (ra.getDigitalflag()==1){
            result = Math.round(Sum/num);
        }else {
            result = FormatResult.setfloat(Sum/(float)num,2);
        }

        ra.setVf(ra.getVs());
        ra.setVs(ra.getVt());
        ra.setVt(String.valueOf(FormatResult.setfloat(Float.parseFloat(tmp),2)));
        ra.setTmp(String.valueOf(result));

        return ra;
    }
}